package com.sgzn.sgpay.web.controller;

import com.sgzn.sgpay.enums.DeviceOperateTypeEnum;
import com.sgzn.sgpay.model.request.*;
import com.sgzn.sgpay.model.request.util.PageQueryParam;
import com.sgzn.sgpay.model.response.SgsResultResponse;
import com.sgzn.sgpay.model.response.result.DevicePageListResult;
import com.sgzn.sgpay.model.response.vo.DeviceInfoVo;
import com.sgzn.sgpay.service.SgsDeviceOperateLogService;
import com.sgzn.sgpay.service.SgsDeviceService;
import com.sgzn.sgpay.web.controller.base.MerchantBaseController;
import com.sgzn.sgpay.web.handle.AssembleCommonHandle;
import com.sgzn.sgpay.web.request.DeviceAddPresentRequest;
import com.sgzn.sgpay.web.request.Impl.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 设备管理控制器
 * @author huangsenguang
 */
@Slf4j
@Controller
@RequestMapping("/merchant/device")
public class DeviceController extends MerchantBaseController {
    @Resource
    private SgsDeviceService sgsDeviceService;

    @Resource
    private SgsDeviceOperateLogService sgsDeviceOperateLogService;
    @Resource
    private AssembleCommonHandle assembleCommonHandle;

    /**
     * 添加设备
     * @param request
     * @return
     */
    @RequestMapping("add")
    @ResponseBody
    public SgsResultResponse<String> addDevice(DeviceAddRequest request) {
        super.setMchInfo(request);
        return this.sgsDeviceService.addDevice(assembleAddRequest(request));
    }

    /**
     * 更新设备信息
     * @param request
     * @return
     */
    @RequestMapping("update")
    @ResponseBody
    public SgsResultResponse<String> updateDevice(DeviceUpdateRequest request) {
        super.setMchInfo(request);
        insertDeviceOpraRecord(request, DeviceOperateTypeEnum.MODIFY.getCode(), DeviceOperateTypeEnum.MODIFY.getDesc());
        return this.sgsDeviceService.updateDevice(assembleUpdateRequest(request));
    }

    /**
     * 更新单价
     * @param request
     * @return
     */
    @RequestMapping("price/update")
    @ResponseBody
    public SgsResultResponse<String> updatePrice(DevicePriceUpdateRequest request) {
        super.setMchInfo(request);
        insertDeviceOpraRecord(request, DeviceOperateTypeEnum.MODIFY.getCode(), DeviceOperateTypeEnum.MODIFY.getDesc());
        return this.sgsDeviceService.updateDevice(assemblePriceUpdateRequest(request));
    }

    /**
     * (批量)删除设备
     * @param request
     * @return
     */
    @RequestMapping("delete")
    @ResponseBody
    public SgsResultResponse<String> deleteDevice(DeviceDeleteRequest request) {
        super.setMchInfo(request);
        return this.sgsDeviceService.deleteDevice(assembleDeleteRequest(request));
    }

    /**
     * 查询设备的信息
     * @param request
     * @return
     */
    @RequestMapping("query")
    @ResponseBody
    public SgsResultResponse<DeviceInfoVo> queryDeviceInfo(DeviceQueryByIdRequest request) {
        super.setMchInfo(request);
        return this.sgsDeviceService.queryMchDeviceInfo(assembleQueryRequest(request));
    }

    /**
     * 分页查询设备的信息
     * @param request
     * @return
     */
    @RequestMapping("queryPage")
    @ResponseBody
    public SgsResultResponse<DevicePageListResult> queryPage(DeviceQueryPageRequest request) {
        super.setMchInfo(request);
        return this.sgsDeviceService.queryPageList(assembleSgsDeviceQueryPageRequest(request));
    }

    /**
     * 查询可用场地编码列表
     * @param request
     * @return
     */
    @RequestMapping("queryAvailCode")
    @ResponseBody
    public SgsResultResponse<List<String>> queryAvailCode(DeviceAvailCodeQueryRequest request) {
        super.setMchInfo(request);
        return this.sgsDeviceService.queryAvailCode(assembleSgsDeviceAvailCodeQueryRequest(request));
    }

    /**
     * 解绑设备
     * @return
     */
    @RequestMapping("unbind")
    @ResponseBody
    public SgsResultResponse<String> unbindDevice(DeviceUnbindRequest request) {
        super.setMchInfo(request);
        List<String> ids = new ArrayList<>(1);
        ids.add(request.getDeviceId());
        SgsDeviceDeleteRequest deviceDelReq = new SgsDeviceDeleteRequest();
        deviceDelReq.setIds(ids);
        assembleCommonHandle.assemble(request,deviceDelReq);
        return sgsDeviceService.deleteDevice(deviceDelReq);
    }



    /**
     * 添加设备礼品
     * @return
     */
    @RequestMapping("addDevicePresent")
    @ResponseBody
    public SgsResultResponse<String> addDevicePresent(DeviceAddPresentRequest  request) {
        SgsDeviceBindPresentRequest sgsDeviceBindPresentRequest = new SgsDeviceBindPresentRequest();
        sgsDeviceBindPresentRequest.setDeviceId(request.getDeviceId());
        sgsDeviceBindPresentRequest.setPresentId(request.getPresentId());
        sgsDeviceBindPresentRequest.setPresentNumber(request.getPresentNumber());
        assembleCommonHandle.assemble(request,sgsDeviceBindPresentRequest);
        return sgsDeviceService.bindPresent(sgsDeviceBindPresentRequest);
    }



    /**
     * 组装设备新增请求
     * @param deviceRequest
     * @return
     */
    private SgsDeviceAddRequest assembleAddRequest(DeviceAddRequest deviceRequest) {
        SgsDeviceAddRequest request = new SgsDeviceAddRequest();
        this.assembleCommonHandle.assemble(deviceRequest, request);
        request.setDeviceId(deviceRequest.getDeviceId());
        request.setSiteNum(deviceRequest.getSiteNum());
        request.setDeviceType(deviceRequest.getDeviceType());
        request.setShopId(deviceRequest.getShopId());
        request.setPrice(deviceRequest.getPrice());
        return request;
    }

    /**
     * 组装查询和删除请求
     * @param deviceRequest
     * @return
     */
    private SgsDeviceDeleteRequest assembleDeleteRequest(DeviceDeleteRequest deviceRequest) {
        SgsDeviceDeleteRequest sgsDeviceDeleteRequest = new SgsDeviceDeleteRequest();
        this.assembleCommonHandle.assemble(deviceRequest, sgsDeviceDeleteRequest);
        sgsDeviceDeleteRequest.setIds(deviceRequest.getIds());
        return sgsDeviceDeleteRequest;
    }

    /**
     * 组装查询和删除请求
     * @param deviceRequest
     * @return
     */
    private SgsDeviceMchQueryRequest assembleQueryRequest(DeviceQueryByIdRequest deviceRequest) {
        SgsDeviceMchQueryRequest sgsDeviceMchQueryRequest = new SgsDeviceMchQueryRequest();
        this.assembleCommonHandle.assemble(deviceRequest, sgsDeviceMchQueryRequest);
        sgsDeviceMchQueryRequest.setDeviceId(deviceRequest.getDeviceId());
        return sgsDeviceMchQueryRequest;
    }

    /**
     * 组装设备更新请求
     * @param deviceRequest
     * @return
     */
    private SgsDeviceUpdateRequest assembleUpdateRequest(DeviceUpdateRequest deviceRequest) {
        SgsDeviceUpdateRequest request = new SgsDeviceUpdateRequest();
        this.assembleCommonHandle.assemble(deviceRequest, request);
        request.setDeviceId(deviceRequest.getDeviceId());
        request.setDeviceName(deviceRequest.getDeviceName());
        request.setShopId(deviceRequest.getShopId());
        request.setSiteNum(deviceRequest.getSiteNum());
        request.setPrice(deviceRequest.getPrice());
        request.setDeviceType(deviceRequest.getDeviceType());
        return request;
    }

    /**
     * 组装设备更新请求
     * @param deviceRequest
     * @return
     */
    private SgsDeviceUpdateRequest assemblePriceUpdateRequest(DevicePriceUpdateRequest deviceRequest) {
        SgsDeviceUpdateRequest request = new SgsDeviceUpdateRequest();
        this.assembleCommonHandle.assemble(deviceRequest, request);
        request.setDeviceId(deviceRequest.getDeviceId());
        request.setPrice(deviceRequest.getPrice());
        return request;
    }

    /**
     * 组装设备分页查询参数
     * @param deviceRequest
     * @return
     */
    private SgsDeviceQueryPageRequest assembleSgsDeviceQueryPageRequest(DeviceQueryPageRequest deviceRequest) {
        SgsDeviceQueryPageRequest target = new SgsDeviceQueryPageRequest();
        this.assembleCommonHandle.assemble(deviceRequest, target);
        target.setDeviceId(deviceRequest.getDeviceId());
        target.setMerchantId(deviceRequest.getMchId());
        target.setDeviceName(deviceRequest.getDeviceName());
        target.setShopId(deviceRequest.getShopId());
        target.setState(deviceRequest.getState());
        PageQueryParam pageQueryParam = new PageQueryParam();
        pageQueryParam.setPageSize(Integer.valueOf(deviceRequest.getPageSize()));
        pageQueryParam.setCurrentPage(Integer.valueOf(deviceRequest.getPageNum()));
        target.setPageQueryParam(pageQueryParam);
        return target;
    }

    /**
     * 组装设备更新请求
     * @param deviceRequest
     * @return
     */
    private SgsDeviceAvailCodeQueryRequest assembleSgsDeviceAvailCodeQueryRequest(DeviceAvailCodeQueryRequest deviceRequest) {
        SgsDeviceAvailCodeQueryRequest request = new SgsDeviceAvailCodeQueryRequest();
        this.assembleCommonHandle.assemble(deviceRequest, request);
        request.setShopId(deviceRequest.getShopId());
        return request;
    }


    /**
     * 设备操作记录
     * @param request
     */
    private void insertDeviceOpraRecord(DeviceUpdateRequest request, String operateType, String operateContent) {
        SgsDeviceOperateLogAddRequest sgsDeviceOperateLogAddRequest = new SgsDeviceOperateLogAddRequest();
        sgsDeviceOperateLogAddRequest.setDeviceId(request.getDeviceId());
        assembleCommonHandle.assemble(request,sgsDeviceOperateLogAddRequest);
        sgsDeviceOperateLogAddRequest.setOperateType(operateType);
        sgsDeviceOperateLogAddRequest.setOperateContent(operateContent);
        this.sgsDeviceOperateLogService.add(sgsDeviceOperateLogAddRequest);
    }

    /**
     * 设备操作记录
     * @param request
     */
    private void insertDeviceOpraRecord(DevicePriceUpdateRequest request, String operateType, String operateContent) {
        SgsDeviceOperateLogAddRequest sgsDeviceOperateLogAddRequest = new SgsDeviceOperateLogAddRequest();
        sgsDeviceOperateLogAddRequest.setDeviceId(request.getDeviceId());
        assembleCommonHandle.assemble(request,sgsDeviceOperateLogAddRequest);
        sgsDeviceOperateLogAddRequest.setOperateType(operateType);
        sgsDeviceOperateLogAddRequest.setOperateContent(operateContent);
        this.sgsDeviceOperateLogService.add(sgsDeviceOperateLogAddRequest);
    }
}

